package com.yss.reportworld.controller;

import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.reportworld.service.ICustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import com.yss.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.reportworld.domain.Record;
import com.yss.reportworld.service.IRecordService;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;

/**
 * 版本记录信息Controller
 *
 * @author yss
 * @date 2023-07-06
 */
@Slf4j
@RestController
@RequestMapping("/record")
public class RecordController extends BaseController {
    @Autowired
    private IRecordService recordService;
    @Autowired
    private ICustomerService customerService;

    /**
     * 查询版本记录信息列表
     */
    @RequiresPermissions("reportworld:record:list")
    @GetMapping("/list")
    public TableDataInfo list(Record record) {
        startPage();
        List<Record> list = recordService.selectRecordList(record);
        return getDataTable(list);
    }

    /**
     * 初始化 加载
     */
    @RequiresPermissions("reportworld:record:list")
    @PutMapping("/init")
    public AjaxResult init() {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("customerTree", customerService.selectCustomerTreeList());
        return ajax;
    }

    /**
     * 导出版本记录信息列表
     */
    @RequiresPermissions("reportworld:record:export")
    @Log(title = "版本记录信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Record record) {
        List<Record> list = recordService.selectRecordList(record);
        EasyExeclUtil.export(response, list, Record.class);
    }

    /**
     * 获取版本记录信息详细信息
     */
    @RequiresPermissions("reportworld:record:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        Record record = new Record();
        record.setId(id);
        List<Record> list = recordService.selectRecordList(record);
        if (StringUtils.isNotNull(list)) {
            return success(list.get(0));
        }
        return success();
    }

    /**
     * 新增版本记录信息
     */
    @RequiresPermissions("reportworld:record:add")
    @Log(title = "版本记录信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated Record record) {
        recordService.saveRecord(record);
        return success();
    }

    /**
     * 修改版本记录信息
     */
    @RequiresPermissions("reportworld:record:edit")
    @Log(title = "版本记录信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated Record record) {
        record.setUpdateTime(new Date());
        record.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(recordService.updateById(record));
    }

    /**
     * 删除版本记录信息
     */
    @RequiresPermissions("reportworld:record:remove")
    @Log(title = "版本记录信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(recordService.removeByIds(ids));
    }
}
